#ifndef _ENTRY_HPP
#define _ENTRY_HPP 1

#include <vector>
#include <stdint.h>
#include <boost/shared_ptr.hpp>
#include "../constants.hpp"
#include "Digest.hpp"


namespace BFT
{
	namespace messages
	{

		class Entry
		{

		  public:
		  
		  Entry(uint32_t _client, uint32_t _req, boost::shared_ptr<Digest> _digest); //
		  
		  Entry(uint32_t _client, uint32_t _req, boost::shared_ptr<char> _command, uint32_t _command_length); //
		  

		  static boost::shared_ptr<Entry> fromBytes(boost::shared_ptr<char> _bytes, uint32_t _bytes_length, uint32_t _offset);
		  
		  static boost::shared_ptr<std::vector< boost::shared_ptr<Entry> > > getList(boost::shared_ptr<char> _bytes, uint32_t _bytes_length, uint32_t _e_count);
		
		  static uint32_t computeSize(uint32_t _command_size);

		  bool hasDigest();
		  
		  uint32_t getClient();
		  
		  uint32_t getRequestId();
		  
		  boost::shared_ptr<char> getCommand();
		  
		  uint32_t getCommandSize();
		  
		  boost::shared_ptr<Digest> getDigest();
		  
		  boost::shared_ptr<Digest> getMyDigest();

		  boost::shared_ptr<char> getBytes(int& _bytes_length);
		  
		  uint32_t getSize();

		  bool matches(boost::shared_ptr<Digest> _digest);
		  
		  bool equals(boost::shared_ptr<Entry> _entry);
		  
		  void printInfo();
		  
		  protected:
		  
		  uint32_t clientId;
		  uint32_t requestId;
		  boost::shared_ptr<char> command;
		  uint32_t command_size;
		  boost::shared_ptr<char> bytes;
		  boost::shared_ptr<Digest> digest;
		  boost::shared_ptr<Digest> my_digest;
		  bool has_digest;

		};


		inline bool Entry::hasDigest() {return has_digest;}

		inline uint32_t Entry::getClient() {return clientId;}

		inline uint32_t Entry::getRequestId() {return requestId;}

		inline boost::shared_ptr<char> Entry::getCommand() {return command;}

		inline uint32_t Entry::getCommandSize() {return command_size;}

		inline boost::shared_ptr<Digest> Entry::getDigest() {return digest;}

		inline uint32_t Entry::getSize() {return ( (hasDigest()?DIGEST_SIZE:command_size) + REQUEST_ID_SIZE + CLIENT_ID_SIZE + COMMAND_SIZE_SIZE + COMMAND_DIGEST_FLAG_SIZE); }

	} //namespace messages
} // namespace BFT




#endif
